#include "stdafx.h"
#include "UserDataLua.h"
#include "Message/MsgLogin.h"
#include "Message/MsgPlayer.h"
#include "Lua/LuaWrapper.h"
#include "UserDataScene.h"

TCLI_API UserDataLua* gUser = NULL;

UserDataLua*
UserDataLua::instance()
{
	static UserDataLua* s_instance;
	if (s_instance==NULL)
	{
		s_instance = new UserDataLua;
	}
	return s_instance;
}



int lua_c_auth(lua_State* l)
{
	if (lua_gettop(l)==1)
	{
		MsgAuthRequest* pMsg = new MsgAuthRequest;
		pMsg->name = lua_tostring(l, -2);
		pMsg->pwd = lua_tostring(l, -1);
	}
	else
	{
		LOG_ERROR("LuaC, function login.auth lack of parameter: account, passworld");
	}
	return 0;
}

int lua_c_selectsvr(lua_State* l)
{
	if (lua_gettop(l)==1)
	{
		MsgSelectSvrRequest* pMsg = new MsgSelectSvrRequest;
	}
	else
	{
		LOG_ERROR("LuaC, function login.select lack of parameter: account, passworld");
	}
	return 0;
}

int lua_c_login(lua_State* l)
{
	if (lua_gettop(l)==1)
	{
		int aid = lua_tointeger(l, 1);
		MsgLoginRequest* pMsg  = new MsgLoginRequest( );
		pMsg->authid = aid;
		UserDataLua::instance()->wpSvcNet->postMsg(pMsg);
	}
	else
	{
		LOG_ERROR("LuaC, function login.login lack of parameter: aid");
	}
	return 0;
}

int lua_c_enter_game(lua_State* l)
{
	if (lua_gettop(l)==1)
	{
		int pid = lua_tointeger(l, 1);
		MsgEnterGameRequest* pMsg  = new MsgEnterGameRequest( );
		pMsg->pid = pid;
		UserDataLua::instance()->pid = pid;
		UserDataLua::instance()->wpSvcNet->postMsg(pMsg);
	}
	else
	{
		LOG_ERROR("LuaC, function login.select lack of parameter: pid");
	}
	return 0;
}

int lua_c_createplayer(lua_State* l)
{
	if (lua_gettop(l)>=4)
	{
		int pid = lua_tointeger(l, 1);
		MsgCreatePlayerRequest* pMsg  = new MsgCreatePlayerRequest;
		pMsg->name = lua_tostring(l, -4);
		pMsg->sex = lua_tointeger(l, -3);
		pMsg->hero = lua_tointeger(l, -2);
		pMsg->headpic = lua_tointeger(l, -1);
		UserDataLua::instance()->wpSvcNet->postMsg(pMsg);
	}
	else
	{
		LOG_ERROR("LuaC, function login.create lack of parameter: name, sex, hero, headpic");
	}
	return 0;
}

int lua_c_deleteplayer(lua_State* l)
{
	if (lua_gettop(l)==1)
	{
		int pid = lua_tointeger(l, 1);
		MsgDeletePlayerRequest* pMsg  = new MsgDeletePlayerRequest( );
		pMsg->pid = pid;
		UserDataLua::instance()->wpSvcNet->postMsg(pMsg);
	}
	else
	{
		LOG_ERROR("LuaC, function login.delete lack of parameter: pid");
	}
	return 0;
}

static const luaL_Reg login_funcs[] = {
	{"auth", lua_c_auth},
	{"select", lua_c_selectsvr},
	{"login", lua_c_login},
	{"enter", lua_c_enter_game},
	{"create", lua_c_createplayer},
	{"delete", lua_c_deleteplayer},
	{NULL, NULL}
};


void
UserDataLua::initialize()
{
	Lua::instance()->load("login", login_funcs);
	initializeSceneLua();
}
